// pages/attractions/attractions.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchValue: '',
    images: [
      'https://img-u-0.51miz.com/Templet/00/16/62/17/166217_f018fbf00e8f4091d48fee5049370245.jpg-0.jpg',
      "https://ts1.tc.mm.bing.net/th/id/R-C.661a8d3d905809462bdda3f459e33dd3?rik=tom4zNc%2fKkuFOg&riu=http%3a%2f%2fimg.daimg.com%2fuploads%2fallimg%2f240326%2f1-240326161K1.jpg&ehk=raQqGq9YLD6RDhgrTg4cvFfHjvlL4JwRMggyQyYotHA%3d&risl=&pid=ImgRaw&r=0"
    ],
   // 全部景点选项
   categoryOptions: [
    { text: '全部景点', value: 0 },
    { text: '自然景观', value: 1 },
    { text: '历史文化遗址', value: 2 },
    {text: '游乐场', value: 3}
  ],
  categoryValue: 0, // 默认选中“全部景点”

  // 全城选项
  areaOptions: [
    { text: '位置距离', value: 0 },
    { text: '10km内', value: 1 },
    { text: '20km内', value: 2 }
  ],
  areaValue: 0, // 默认选中“全城”

  // 智能排序选项
  sortOptions: [
    { text: '智能排序', value: 0 },
    { text: '评分排序', value: 1 },
    { text: '价格排序', value: 2 }
  ],
  sortValue: 0, // 默认选中“智能排序”
  attractions: [],
  },
  onClickLeft(){
    wx.navigateBack();
  },
  stepdetail(e){
      const id = e.currentTarget.dataset.id;
      const buyTicketRank = e.currentTarget.dataset.rank
      // console.log(e)
      wx.navigateTo({
        url: `/pages/attractionsDetail/attractionsDetail?id=${id}&buyTicketRank=${buyTicketRank}`,
      })
  },
  onInput(e) {
    this.setData({ searchValue: e.detail });
    console.log(this.data.searchValue)
  },
  //搜索
  onSearch() {
    const query = this.data.searchValue.trim();
    if (query) {
      wx.request({
        url: 'http://localhost:8080/system/spot/wxlist',
        header: {
          Authorization: app.globalData.token
        },
        data:{name:query},
        success: (res) => {
          if (res.data.code === 200 && res.data.rows.length > 0) {
            console.log(res)
            const formattedAttractions = res.data.rows.map(item => ({
              imageUrl: "http://localhost:8080"+item.imgUrl, // 图片地址
              title: item.name, // 景点名称
              rating: item.star, // 星级评分
              description: `${item.categoryName} | ${item.address}`, 
              price: item.min_price, // 门票价格
              id:item.id
            }));
            this.setData({
              attractions: formattedAttractions
            });
          } else {
            wx.showToast({ title: '暂无景点数据', icon: 'none' });
          }
        },
        fail: () => {
          wx.showToast({ title: '网络请求失败', icon: 'none' });
        }
      });
    } else {
      this.loadAttractions()
    }
  },
  // 智能排序筛选
  onSortChange(e) {
    const value = e.detail;
    console.log("111",e)
    this.setData({ sortValue: value });
  
    let sortedAttractions = [...this.data.attractions]; // 复制原始数据
  
    if (value === 1) {
      // 按评分降序排序
      sortedAttractions.sort((a, b) => b.rating - a.rating);
    } else if (value === 2) {
      // 按价格升序排序
      sortedAttractions.sort((a, b) => a.price - b.price);
    }else{
        this.loadAttractions()
    }
    this.setData({
      attractions: sortedAttractions
    });
  },
  //全部景点筛选
  onCategoryChange(e) {
    const value = e.detail; // 获取选中的值（0, 1, 2）
    this.setData({ categoryValue: value });
  
    let categoryName = '';
    if (value === 1) {
      categoryName = '自然景观';
    } else if (value === 2) {
      categoryName = '历史文化遗址';
    }else if (value === 3){
      categoryName = '游乐场';
    }
  
    // 调用接口传入 categoryName 进行筛选
    wx.request({
      url: 'http://localhost:8080/system/spot/wxlist',
      header: {
        Authorization: app.globalData.token
      },
      data: {
        categoryName: categoryName // 向后端传递分类名用于筛选
      },
      success: (res) => {
        if (res.data.code === 200) {
          const formattedAttractions = res.data.rows.map(item => ({
            imageUrl: "http://localhost:8080" + item.imgUrl,
            title: item.name,
            rating: item.star,
            description: `${item.categoryName} | ${item.address}`,
            price: item.min_price,
            id:item.id
          }));
          this.setData({
            attractions: formattedAttractions
          });
        } else {
          wx.showToast({ title: '暂无景点数据', icon: 'none' });
        }
      },
      fail: () => {
        wx.showToast({ title: '网络请求失败', icon: 'none' });
      }
    });
  },
  //页面初始化加载景点列表
  loadAttractions() {
    wx.request({
      url: 'http://localhost:8080/system/spot/wxlist',
      header: {
        Authorization: app.globalData.token
      },
      success: (res) => {
        if (res.data.code === 200 && res.data.rows.length > 0) {
          // console.log(res)
          const formattedAttractions = res.data.rows.map(item => ({
            imageUrl: "http://localhost:8080"+item.imgUrl, // 图片地址
            title: item.name, // 景点名称
            rating: item.star, // 星级评分
            description: `${item.categoryName} | ${item.address}`, 
            price: item.min_price, // 门票价格
            id:item.id,
            buyTicketRank:item.buyTicketRank
          }));
          this.setData({
            attractions: formattedAttractions
          });
        } else {
          wx.showToast({ title: '暂无景点数据', icon: 'none' });
        }
      },
      fail: () => {
        wx.showToast({ title: '网络请求失败', icon: 'none' });
      }
    });
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.loadAttractions();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})